package com.dingwen.treasure.core.util;

import lombok.experimental.UtilityClass;

import java.util.HashMap;
import java.util.Map;

/**
 * 　LogContext
 * 　@author dingwen
 * 　@date 2022/8/29
 */
@UtilityClass
public class LogContext {

    /**
     * 参数集合
     */
    private static ThreadLocal<Map<String, String>> MAP = new ThreadLocal<>();

    static {
        MAP.set(new HashMap<>());
    }

    /**
     * 设置值
     *
     * @param methodKey 方法key
     * @param value     价值
     */
    public void set(String methodKey, String value) {
        MAP.get().put(methodKey, value);
    }

    /**
     * 得到
     *
     * @param methodKey 方法key
     * @return {@link String}
     */
    public String get(String methodKey) {
        return MAP.get().get(methodKey);
    }

    /**
     * 删除
     */
    public void remove(String methodKey) {
        Map<String, String> map = MAP.get();
        map.remove(methodKey);
        if (map.isEmpty()) {
            MAP.remove();
        }
    }
}
